package cn.tanghom.baiding.core.service.impl;

import cn.tanghom.baiding.core.dao.BaseDao;
import cn.tanghom.baiding.core.page.Page;
import cn.tanghom.baiding.core.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.List;
import java.util.UUID;


@Service
@Transactional
public abstract class BaseServiceImpl<T> implements BaseService<T> {
	@Autowired
    protected BaseDao<T> baseMapper;

	@Override
    public int insert(T t){
		String id = UUID.randomUUID().toString();
		Field field;
		try {
			field = t.getClass().getDeclaredField("id");
			field.setAccessible(true);
			field.set(t, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return baseMapper.insert(t);

    }
	@Override
    public int update(T t){
    	return baseMapper.update(t);

    }
	@Override
	public T selectByPrimaryKey(Long id){
		return baseMapper.selectByPrimaryKey(id);

	}
	@Override
	public int deleteByT(T[] ts){
		return baseMapper.deleteByT(ts);

	}
	@Override
    public int deleteByPrimaryKeys(Long[] ids){
    	return baseMapper.deleteByPrimaryKeys(ids);
    	
    }
	@Override
	public int deleteByPrimaryKey(Long id){
		return baseMapper.deleteByPrimaryKey(id);
	}

    @Override
	public Page<T> selectTByPage(Page<T> page, T t) {
    	page.setT(t);
		List<T> list=baseMapper.selectTByPage(page);
		page.setItems(list);
		Long count=baseMapper.selectTCountByT(page.getT());
		page.setTotalRecord(count);
		return page;
	}
    @Override
    public List<T> selectTByT(T t){
		return baseMapper.selectTByT(t);
    }
    
    @Override
    public T selectTByTOne(T t){
    	List<T> tOne=baseMapper.selectTByT(t);
    	if(tOne.size()>0){
    		return tOne.get(0);
    	}else{
    		return null;
    	}
		
    }
	
}
